﻿/*
 * Copyright (c) 2010, Sergey Loktin (mailto://shadowconsp@gmail.com)
 * Licensed under The MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace MoonyDesk
{
    /// <summary>
    /// Interaction logic for DesktopWindow.xaml
    /// Main window fo each plugin
    /// </summary>
    public partial class DesktopWindow : Window
    {
        private IntPtr _hwnd;

        public static readonly DependencyProperty MovableProperty = DependencyProperty.Register(
          "Movable", typeof(bool), typeof(DesktopWindow), new PropertyMetadata(true));
        /// <summary>
        /// Gets or sets value that indicates whether window is movable by mouse
        /// </summary>
        public bool Movable
        {
            get { return (bool)this.GetValue(MovableProperty); }
            set 
            {
                this.SetValue(MovableProperty, value);
                SetMovable(value);
            }
        }

        public static readonly DependencyProperty Topmost2Property = DependencyProperty.Register(
          "Topmost", typeof(bool), typeof(DesktopWindow), new PropertyMetadata(false));
        /// <summary>
        /// Gets or sets value that indicates whether window is topmost
        /// </summary>
        public bool Topmost2
        {
            get { return (bool)this.GetValue(Topmost2Property); }
            set
            {
                this.SetValue(Topmost2Property, value);
                SetTopmost(value);
            }
        }

        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll")]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
           int Y, int cx, int cy, uint uFlags);

        public DesktopWindow()
        {
            InitializeComponent();
            //default
            this.Width = 300;
            this.Top = 10;
            this.Left = 10;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _hwnd = new WindowInteropHelper(this).Handle;
            SetWindowLong(_hwnd, -20, GetWindowLong(_hwnd, -20) | 0x00000080);//tool window (isn't shown in Task Switcher)
        }

        private void Window_ContentRendered(object sender, EventArgs e)
        {
            SetWindowPos(_hwnd, new IntPtr(1), 0, 0, 0, 0, (uint)(1 | 2 | 10));//send to back
            SetWindowPos(_hwnd, new IntPtr(1), 0, 0, 0, 0, (uint)(1 | 2 | 10));//first call doesn't work in wpf? send to back again
        }

        private void SetMovable(bool Movable)
        {
            if (Movable)
                SetWindowLong(_hwnd, -20, GetWindowLong(_hwnd, -20) & ~0x00000020);//window isn't transparent for clicks
            else
                SetWindowLong(_hwnd, -20, GetWindowLong(_hwnd, -20) | 0x00000020);//window is transparent for clicks
        }

        private void SetTopmost(bool Topmost)
        {
            if (Topmost)
            {
                SetWindowPos(_hwnd, new IntPtr(-1), 0, 0, 0, 0, (uint)(1 | 2 | 10));//topmost window
            }
            else
            {
                SetWindowPos(_hwnd, new IntPtr(1), 0, 0, 0, 0, (uint)(1 | 2 | 10));//send to back
                SetWindowPos(_hwnd, new IntPtr(1), 0, 0, 0, 0, (uint)(1 | 2 | 10));//first call doesn't work in wpf? send to back again
            }
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
}
